<template>
    <Form ref="formValidate" class="exform" :model="formValidate" :label-width="80" :rules="ruleValidate">
		<FormItem label="项目名称" prop="name.zh"  style="width:50%">
			<Input v-model="formValidate.name.zh" placeholder="项目名称" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="资助单位" prop="fundunit.zh"  style="width:50%">
			<Input v-model="formValidate.fundunit.zh" placeholder="资助单位" style="width:80%">
			</Input>
		</FormItem>
		<FormItem label="开始时间" prop="time"  style="width:50%">

			<DatePicker placement="right" v-model="formValidate.time" type="month"  placeholder="请选择时间" style="width: 200px" transfer></DatePicker>
			
		</FormItem>
		<FormItem label="资助年限" prop="fundyear" style="width:50%" >
			<Select v-model="formValidate.fundyear"  style="width:80%" transfer>
			<Option v-for="item in formItem.financedlife" :value="item.dictionary_id" :key="item.dictionary_id"> {{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Option>
			</Select>
		</FormItem>
		<FormItem label="资助金额" prop="projectmoney"  style="width:50%">
			<Input v-model="formValidate.projectmoney" placeholder="资助金额" style="width:80%">
			</Input>（万元） 
		</FormItem>
			<FormItem label="资助内容" prop="fundcontent.zh" style="width:50%">
				<Input v-model="formValidate.fundcontent.zh" style="width:80%" type="textarea" :autosize="{minRows: 2,maxRows: 4}" placeholder="资助内容"></Input>
			</FormItem>
		<div style="width:90%;">
			<FormItem style="width:100%;">
			<Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
			</FormItem>
		</div>
		<div style="clear:both"></div>
		<Spin size="large" fix v-if="spinShow"></Spin>
    </Form>
</template>

<script>

  export default {

    props: {
        teacher_id: {
            type: [Number,String],
            default: null
        },
        infoId: {
            type: [Boolean,String,Number],
            default: false
        }
    },
    data () {
      	return {
            infoUrl:`api/teacher/v1/afbsqualification/info/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式
            spinShow:false,//加载动画

			infoOld:null,
			formItem:[],

			formValidate: {
				teacher_id:this.teacher_id,
				projectmoney:'',// 资助金额 
				fundyear:'',    //资助年限
				time:'',     //开始时间
				shenhe:1,       //审核
				fundunit:{
					zh:'',
					mn:'',
					en:'',
				},    //资助单位
				name:{
					zh:'',
					mn:'',
					en:'',
				},    //项目名称
				fundcontent:{
					zh:'',
					mn:'',
					en:'',
				},    //资助内容
			},
			ruleValidate: {
				// 开始时间
				time: [
					{ required: true, type: 'date', message: '请选择开始时间', trigger: 'blur' }
				],
				//资助年限
				fundyear: [
					{ type:'number',required: true, message: '请选择资助年限', trigger: 'change' }
				],
				//资助金额
				projectmoney: [
					{ required: true, message: '请填写资助金额', trigger: 'blur' }
				],
				//资助单位
				'fundunit.zh': [
					{ required: true, message: '请填写资助单位', trigger: 'blur' }
				],
				//资助内容
				'fundcontent.zh': [
					{ required: true, message: '请填写资助内容', trigger: 'blur' }
				],

				//项目名称
				'name.zh': [
					{ required: true, message: '请填写项目名称', trigger: 'blur' }
				],
			},  
      	}
    },
    methods: {
     
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.spinShow=true//输入框禁用  
            this.$http.get(this.infoUrl).then(val=>{
                this.infoOld = JSON.parse(JSON.stringify(val.data))
                this.formValidate.shenhe = parseInt(this.infoOld.shenhe)//用于默认选的 审核
                this.formValidate.fundyear = parseInt(this.infoOld.fundyear)//用于默认选的 资助年限
                
                this.formValidate.time = this.infoOld.time// 开始时间
                this.formValidate.projectmoney = this.infoOld.projectmoney// 资助金额
                this.formValidate.fundunit = this.infoOld.fundunit// 资助单位
                this.formValidate.name = this.infoOld.name// 项目名称
                this.formValidate.fundcontent = this.infoOld.fundcontent// 资助内容
                this.formValidate.school_id=this.infoOld.school_id;//学校
                this.formValidate.teacher_id=this.infoOld.teacher_id;//教师  
                this.spinShow=false//输入框禁用 
                
            })
			.catch(err => {
				if(err.code && err.code==422) {
					this.$Message.error(err.message);
				}
                this.spinShow=false//输入框禁用 
			});
        },
        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
		handleSubmit (name) {
			this.$refs[name].validate((valid) => {
                if (valid) {       
                    this.spinShow=true//输入框禁用   
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })  
                } else {
                    this.$Message.error('表单验证失败!');
                }
			})
		},
    },
    created() {
        /************************************************************
         * 
         * 查询资格类型
         * 
         ************************************************************/
		this.$http.get('api/dictionary/v1/dictionary/pidquerys?financedlife')
		.then(val=>{
			this.formItem = val.data
			this.spinShow=false//输入框禁用 
		})
		.catch(err => {
			if(err.code && err.code==422) {
				this.$Message.error(err.message);
			}
			this.spinShow=false//输入框禁用 
		});
        if(this.infoId==false){
            // add
            this.UpImgShow=true
            this.saveUrl='api/teacher/v1/afbsqualification/add'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/teacher/v1/afbsqualification/edit/${this.infoId}`
            this.saveType=this.$http.post
        }
    },
  }
</script>
<style lang="less">
@import'./../public.less';
</style>

